import { ChangeDetectorRef, Component, inject, OnInit } from '@angular/core';
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
import { Observable, of } from 'rxjs';

import { OptionsInterface } from '@app/core/services/types';
import { SharedModule } from '@app/shared/shared.module';
import { fnCheckForm } from '@app/utils/tools';
import { BasicConfirmModalComponent } from '@app/widget/base-modal';
import { NzSafeAny } from 'ng-zorro-antd/core/types';
import { NZ_MODAL_DATA, NzModalRef } from 'ng-zorro-antd/modal';

@Component({
  selector: 'app-link-action-setting',
  standalone: true,
  imports: [SharedModule],
  templateUrl: './link-action-setting-modal.component.html',
  styleUrl: './link-action-setting-modal.component.less'
})
export class LinkActionSettingModalComponent extends BasicConfirmModalComponent implements OnInit {
  validateForm!: FormGroup;
  typeList: OptionsInterface[] = [
    { value: 1, label: '跳转链接' },
    { value: 2, label: '自定义代码' },
    { value: 3, label: '返回顶部' }
  ];
  readonly nzModalData: NzSafeAny = inject(NZ_MODAL_DATA);
  constructor(
    override modalRef: NzModalRef,
    private readonly cdr: ChangeDetectorRef,
    private readonly fb: FormBuilder
  ) {
    super(modalRef);
  }

  protected override getCurrentValue(): Observable<NzSafeAny> {
    if (!fnCheckForm(this.validateForm)) {
      return of(false);
    }
    return of(this.validateForm.value);
  }

  ngOnInit(): void {
    this.validateForm = this.fb.group({
      type: new FormControl(1),
      linkUrl: new FormControl()
    });
    if (this.nzModalData) {
      this.validateForm.setValue(this.nzModalData);
    }
  }
}
